
#include "CondE.h"
#include "Common/TableDataEx/DuplicateCfg.h"


CondE::CondE() :m_pDuplicate(nullptr), m_pVecCond(nullptr), m_result(-1), m_finish(false), m_limitSec(0), m_CreatureKind(CREATURE_NONE)
{

}

CondE::~CondE()
{

}

bool CondE::init(IDuplicate *pDuplicate, const VEC_INT32 *pCond, const string &strSubCond)
{
	if (nullptr == pDuplicate || nullptr == pCond)
	{
		return false;
	}
	//5，限制时间，玩家死亡
	int32_t isize = pCond->size();
	if (isize < 2)
	{
		LogErrFmtPrint("[logic] CondE::init....isize < 2, dupid:%u, sceneid:%u , isize:%d ", pDuplicate->GetDuplicateID(), pDuplicate->GetSceneID(), isize);
		return false;
	}
	m_pVecCond = pCond;
	m_pDuplicate = pDuplicate;
	//
	m_limitSec = m_pVecCond->at(1);
	
	return true;
}

void CondE::uninit()
{
	m_pDuplicate = nullptr;
	m_pVecCond = nullptr;
	m_finish = false;
	m_result = -1;
	m_limitSec = 0;
	m_CreatureKind = CREATURE_NONE;
}

bool CondE::finishCond()
{
	return m_finish;
}

int32_t CondE::limitSec()
{
	return m_limitSec;
}

int32_t CondE::result()
{
	return m_result;
}

void CondE::onCreatrueDie(uint64_t cid, int8_t creatureKind)
{
	_UNUSE(cid);
	if (CREATURE_REBOT == creatureKind || CREATURE_PLAYER == creatureKind)
	{
		if (CREATURE_NONE == m_CreatureKind)
		{
			m_CreatureKind = creatureKind;
		}
	}
}

void CondE::onPlayerLeave(uint64_t cid)
{
	_UNUSE(cid);
	if (CREATURE_NONE == m_CreatureKind)
	{
		m_CreatureKind = CREATURE_PLAYER;
	}
}


void CondE::condUpdate()
{
	if (m_finish)
	{
		return;
	}
	m_result = checkCond();
	if (RET_SUCCESS == m_result || RET_FAIL == m_result)
	{
		m_finish = true;
	}
}

int32_t CondE::checkCond()
{
	if (nullptr == m_pVecCond || nullptr == m_pDuplicate)
	{
		return -1;
	}
	int32_t isize = m_pVecCond->size();
	if (isize < 2)
	{
		return -1;
	}
	int32_t ret = -1;
	int32_t sec = m_pVecCond->at(1);
	uint64_t curTime = g_GetGlobalServerTime()->UnixSec();
	int32_t interSec = (int32_t)(curTime - m_pDuplicate->StartBrushTime());

	/*
	1，指定时间内消灭指定怪物序列，才能通关；超时失败
	2，指定时间内消灭所有怪物，才能通关；超时失败
	3，消灭所有怪物，才能通关；塔被怪物消灭失败
	4，达到指定时间点，怪物传送的数量小于目标值；怪物传送的数量达到目标值，或有BOSS进行传送，失败
	5，竞技场专用：
	倒计时结束前，挑战者死亡，挑战者失败；
	倒计时结束前，被挑战者（镜像）死亡，挑战者胜利；
	倒计时结束时，被挑战者（镜像）未死亡，挑战者失败

	1，限制时间，怪物1的ID，怪物2的ID，……
	2，限制时间
	3，塔的ID
	4，限制时间，传送区域ID，目标值，BOSS1的ID，BOSS2的ID，……
	5，限制时间

	*/
	if (CREATURE_NONE == m_CreatureKind)
	{
		if (interSec >= sec) //超时失败
		{
			ret = RET_FAIL;
		}
	}
	else if (CREATURE_PLAYER != m_CreatureKind)
	{
		ret = RET_SUCCESS;
	}
	else
	{
		ret = RET_FAIL;
	}

	return ret;
}